<#include "../head.html"/>
	
<!-- 内容主体区域 -->
	<script type="text/html" id="barDemo">
  	<div class="layui-btn-container">
    	<button class="layui-btn layui-btn-sm" id="saveData">保存</button>
  	</div>
	</script>
	<input type="hidden" name="roleid" id="roleid" value="${roleid}">
	<table class="layui-hidden" id="treeTable" lay-filter="treeTable"></table>
		
	<script src="/statics/layui/layui.js"></script>
	<script>
		//JavaScript代码区域
		var editObj=null,ptable=null,treeGrid=null,tableId='treeTable',layer=null;
		var ids=${ids}
	    layui.config({
	        base: '/statics/layui/extend/'
	    }).extend({
	    	treeGrid:'treeGrid'
	    }).use(['jquery','treeGrid','layer'], function(){
	        var $=layui.jquery;
	        treeGrid = layui.treeGrid;//很重要
	        layer=layui.layer;
	        ptable=treeGrid.render({
	            id:tableId
	            ,elem: '#'+tableId
	            ,url:'/resource/list.jhtml'
	            ,cellMinWidth: 100
	            ,idField:'id'//必須字段
                ,treeId:'id'//树形id字段名称
                ,treeUpId:'pid'//树形父id字段名称
                ,treeShowName:'name'//以树形式显示的字段
	            ,toolbar: '#barDemo'
                 ,isFilter:false
                 ,iconOpen:false//是否显示图标【默认显示】
                 ,isOpenDefault:true//节点默认是展开还是折叠【默认展开】
                 ,loading:true
                 ,isPage:false
	            ,cols: [[
	            	{type:'checkbox',sort:true}
	                ,{field:'name', width:300, title: '菜单名称'}
	                ,{field:'id',width:100, title: 'id'}
	                ,{field:'pid', title: 'pid'}
	                
	            ]]
	            ,parseData:function (res) {//数据加载后回调
	                return res;
	            }
	            ,onClickRow:function (index, o) {
	                console.log(index,o,"单击！");
	            }
	            ,onDblClickRow:function (index, o) {
	                //console.log(index,o,"双击");
	            }
	            ,onCheck:function (obj,checked,isAll) {//复选事件
	                //console.log(obj,checked,isAll,"复选");
	            }
	            ,onRadio:function (obj) {//单选事件
	                //console.log(obj,"单选");
	            }
	            ,done: function(res, curr, count){
	    	    	var table_data=res.data;
	    	    	for(var i=0;i< res.data.length;i++){
	                    for (var j = 0; j < ids.length; j++) {
	                        //数据id和要勾选的id相同时checkbox选中
	                        //alert(res.data[i].id +"==="+ ids[j]);
	                        if(res.data[i].id == ids[j])
	                        {
	                        	treeGrid.setCheckStatus(tableId,"id",ids.join(","));
	                            
	                        }
	                    }
	                }
	    	    }
	        });

	        $("#saveData").click(function(){
	        	var checkStatus = treeGrid.checkStatus('treeTable')
	  	      ,data = checkStatus.data;
		  		var ids=[];
		  		for(var i in data){
		  			console.log(data[i]);
		  			var fats=treeGrid.treeFindUpDatas('treeTable',data[i]);
		  			for(var j in fats){
			  			if(ids.indexOf(fats[j].id)==-1){
			  				ids.push(fats[j].id);
			  			}
		  				
		  			}
		  			if(ids.indexOf(data[i].id)==-1){
		  				ids.push(data[i].id);
		  			}
		  		}
		  		if(ids==''||ids.length==0){
		  			layer.alert("请选择！");
		  			return false;
		  		}
		  		alert(ids);
		  		$.ajax({  
				    type: "POST",  
				    url: "/role/saveResource.jhtml",  
				    data: { "resourceids":  ids.join(","),"roleid":$("#roleid").val() },  
				    dataType: "json",  
				    success: function (data) {  
				    	if(data.state==0){
							layer.alert(
									data.message,
									{
										icon: 1,    // icon
										yes:function(){
											parent.location.reload();
										}
										
									}
							);
						}else{
							layer.alert(data.message);
						}
				    }  
				});
	        });
	    });
	    function del(id) {
	        layer.confirm("你确定删除数据吗？如果存在下级节点则一并删除，此操作不能撤销！", {icon: 3, title:'提示'},
	            function(index){//确定回调
		        	$.ajax({
		    			type:"post",
		    			url : "/resource/doDeleteRole",
		    			data : {id:id},
		    			dataType:"json",
		    			success : function(data) {
		    				if(data.state==0){
		    					layer.alert(
		    							data.message,
		    							{
		    								icon: 1,    // icon
		    								yes:function(){
		    									location.reload();
		    								}
		    								
		    							}
		    					);
		    				}else{
		    					alert(data.message)					
		    				}
		    			}
		    		});
	            },function (index) {//取消回调
	               layer.close(index);
	            }
	        );
	    }


	    var i=1000;
	    //添加
	    function edit(id) {
	    	layer.open({
	    		  type: 2, 
	    		  area: ['500px', '450px'],
	    		  content: '/resource/edit.jhtml?id='+id //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
	    	
	    		}); 
	    }
	    
	    function add() {
	    	layer.open({
	    		  type: 2, 
	    		  area: ['500px', '450px'],
	    		  content: '/resource/add.jhtml' //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
	    		}); 
	    }

	    function print() {
	        console.log(treeGrid.cache[tableId]);
	        var loadIndex=layer.msg("对象已打印，按F12，在控制台查看！", {
	            time:3000
	            ,offset: 'auto'//顶部
	            ,shade: 0
	        });
	    }
		
	</script>
